package com.ys.service.finance.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.finance.TcwFinancialTransactionType;
import com.ys.entity.finance.TcwManageTransact;
import com.ys.entity.sys.TsUserChange;
import com.ys.mapper.finance.TcwManageTransactMapper;
import com.ys.service.finance.TcwManageTransactService;
import com.ys.util.CheckUtil;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service("tcwManageTransactService")
public class TcwManageTransactServiceImpl implements TcwManageTransactService {
	private static final Logger logger = LoggerFactory.getLogger(TcwManageTransactServiceImpl.class);

	@Autowired
	private TcwManageTransactMapper tcwManageTransactMapper;

	@Autowired
	private CheckUtil checkUtil;


	/*
	 * 新增
	 */
	@Override
	public R add(Map<String, Object> params) throws Exception {

		TcwManageTransact tcwManageTransact = JSON.parseObject(JSON.toJSONString(params), TcwManageTransact.class);
		String loginUserId = ShiroUtils.getUserId();
		TsUserChange tsUserChange = new TsUserChange().setUserId(loginUserId).setStatus(1).queryFirst();
		tcwManageTransact.setStatus(1);
		String firstType = params.get("firstType").toString();
		String secondType = params.get("secondType").toString();
		if (firstType == null || secondType == null) {
			return R.error("请补充必填项");
		} else if (new TcwManageTransact().setFirstType(firstType).setSecondType(secondType).count() > 0) {
			return R.error("已新增过该数据，请重新组合");
		}
		tcwManageTransactMapper.insert(tcwManageTransact);
		return R.ok(AddDto.closeAndReload());
	}



	@Override
	public R edit(Map<String, Object> params) throws Exception {
		logger.info("申请修改传参:"+params);
		TcwManageTransact tcwManageTransact = JSON.parseObject(JSON.toJSONString(params), TcwManageTransact.class);
		tcwManageTransactMapper.update(tcwManageTransact);
		return R.ok();
	}

	

}
